﻿using Ah.Testudin.Engine.Events;

using Microsoft.Xna.Framework;

namespace Ah.Testudin.Engine.Actors.Components
{
    public class HealthComponent : ActorComponentBase
    {
        #region Private Member Variables

        private readonly int _maxHealth;

        private int _currentHealth;

        #endregion

        #region Properties

        public int CurrentHealth
        {
            get
            {
                return _currentHealth;
            }
        }

        public int MaxHealth
        {
            get
            {
                return _maxHealth;
            }
        }

        #endregion

        #region Constructors

        public HealthComponent(
            int maxHealth,
            Actor owner,
            IEventManager eventManager)
            : base(owner, eventManager)
        {
            _maxHealth = maxHealth;

            _currentHealth = maxHealth;
        }

        #endregion

        #region ActorComponentBase

        public override void Update(GameTime gameTime)
        {
            // Do Nothing
        }

        #endregion

        #region Public Methods

        public void ApplyDamage(int damage)
        {
            if ((_currentHealth - damage) < 0)
            {
                _currentHealth = 0;

                _eventManager.QueueEvent(
                    new ActorDestroyedEvent(
                        this.Owner.Id));
            }
            else
            {
                _currentHealth -= damage;
            }
        }

        #endregion
    }
}
